package com.qn.example.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qn.example.mapper.CategoryMapper;
import com.qn.example.mapper.SetmealDishMapper;
import com.qn.example.mapper.SetmealMapper;
import com.qn.example.pojo.*;
import com.qn.example.pojo.dto.DishDto;
import com.qn.example.pojo.dto.SetmealDto;
import com.qn.example.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 师明
 * @date 2023/5/16 9:35
 */
@Service
public class SetmealServiceImpl implements SetmealService {


    //注入持久层的对象
    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    //    @Transactional(rollbackFor = Exception.class)
    @Override
    @CacheEvict(value = "setmealCache",allEntries =true ) // allEntries =true 该名称空间下的所有缓存全部清楚
    public void saveSetmeal(SetmealDto setmealDto) {
        //补数据
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateTime(LocalDateTime.now());
        //补状态
        setmealDto.setStatus(1);
        //调用setmealDao的添加方法
        setmealMapper.saveSetmeal(setmealDto);
        //将该套餐中的所有菜品获取
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //将套餐和菜品关联
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(0);
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
        }
        //批量插入当前套餐中的所有菜品
        setmealDishMapper.batchSaveSetmealDish(setmealDishes);
    }

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //开启分页
        PageHelper.startPage(page, pageSize);
        //2. 查询页面的数据 List<Dish>
        List<Setmeal> dishList = setmealMapper.findByPage(name);
        //3. 构建一个PageInfo
        PageInfo<Setmeal> pageInfo = new PageInfo<>(dishList);
        //使用jdk8的Stream流， Stream的作用：简化集合与数组的操作   map方法：对集合的元素进行类型转换
        List<SetmealDto> SetmealhDtoList = dishList.stream().map(Setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            //如果两个实体类的属性名与类型都一致的情况，我们可以使用Spring提供的工具类
            BeanUtils.copyProperties(Setmeal, setmealDto); //属性拷贝
            //根据菜品的类别的id查询类别
            Category category = categoryMapper.findById(Setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        //5. 把PageInfo转换为Page对象
        Page<SetmealDto> setmealDtoPage = new Page<>(SetmealhDtoList, pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());
        return setmealDtoPage;
    }

    @Override
    public void delete(List<Long> ids) {
        System.err.print("批量删除的菜品id为：");
        ids.forEach(System.err::println);
        //点击删除，先删除套餐菜品关系，最后删除套餐信息！
        setmealDishMapper.deleteBysetmealIds(ids);
        setmealMapper.deleteids(ids);
    }

    @Override
    public void dishStatusByStatus(Integer status, List<Long> ids) {
        //修改时间
        LocalDateTime now = LocalDateTime.now();
        setmealMapper.dishStatusByStatus(status, ids, now);
    }

    @Override
    public Setmeal findById(Long id) {
        return setmealMapper.findById(id);
    }


    @Override
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    public List<SetmealDto> list(Long categoryId, Integer status) {
        List<Setmeal> setmealList = setmealMapper.list(categoryId, status);
        //转成Dto
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            //属性拷贝
            BeanUtils.copyProperties(setmeal, setmealDto);
            //查询套餐对应菜品
            System.err.println("套餐的id：" + setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.findByDishId(setmeal.getId());
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());

        return setmealDtoList;
    }
}
